package xyz.xtt.admin.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.xdevapi.UpdateResult;
import com.tongtong.middleware.client.MiddlewareClient;
import com.tongtong.middleware.vo.response.AccountBalanceRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import xyz.xtt.admin.entity.TblAdminUser;
import xyz.xtt.admin.mapper.AdminUserMapper;
import xyz.xtt.admin.request.UserRequest;
import xyz.xtt.admin.service.IAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.xtt.admin.utils.Md5Util;
import xyz.xtt.admin.vo.AdminUserVo;
import xyz.xtt.common.response.ApiResult;
import java.math.BigInteger;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
@Slf4j
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, TblAdminUser> implements IAdminUserService {
    @Autowired
    private MiddlewareClient middlewareClient;

    @Override
    public TblAdminUser getByName(String username) {
        return baseMapper.getByName(username);
    }

    @Override
    public Page<AdminUserVo> userList(Integer current, Integer pageSize) {
        return baseMapper.userList(new Page<>(current,pageSize));
    }

    @Override
    public boolean setStatus(Integer id, Integer status) {
        return baseMapper.setStatus(id, status);
    }

    @Override
    public ApiResult<AccountBalanceRes> getBalance() {

        ApiResult<AccountBalanceRes> balance = middlewareClient.mergeAccountBalance();
        log.info("middlewareClient.textAudit response: {}", JSONUtil.toJsonStr(balance));
        return balance;
    }

    @Override
    public boolean update(UserRequest user) {
        if(StringUtils.isNotBlank(user.getPassword())) {
            user.setPassword(Md5Util.getMD5String(user.getPassword()));
        }
        return baseMapper.update(user.getId(), user.getUserName(), user.getNickName(), user.getPassword());
    }
}
